package com.leyou.auth.config;

import com.leyou.auth.utils.RsaUtils;
import lombok.Data;
import org.springframework.boot.context.properties.ConfigurationProperties;

import javax.annotation.PostConstruct;
import java.io.File;
import java.security.PrivateKey;
import java.security.PublicKey;

/**
 * @ClassName JwtProperties
 * @Date 2019/2/25 16:43
 */
@Data
@ConfigurationProperties(prefix = "ly.jwt")
public class JwtProperties {

    private String secret;
    private String pubKeyPath;
    private String priKeyPath;
    private Integer expire;
    
    private PublicKey publicKey;//公钥
    private PrivateKey privateKey;//私钥

    /**
     * 对象一旦创建，就应该读取公钥和私钥
     */
    @PostConstruct
    public void init() throws Exception {
        File pubFile = new File(pubKeyPath);
        File priFile = new File(priKeyPath);
        //如果公钥私钥不存在
        if (!pubFile.exists() || !priFile.exists()) {
            RsaUtils.generateKey(pubKeyPath, priKeyPath, secret);
        }
        this.publicKey = RsaUtils.getPublicKey(pubKeyPath);
        this.privateKey = RsaUtils.getPrivateKey(priKeyPath);
    }
}
